package com.liuli.utils;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;



public class EmailManager {

	private static final String HOST = "smtp.163.com";
    private static final Integer PORT = 25;
    private static final String USERNAME = "liulitry@163.com";
    private static final String PASSWORD = "liuli123456";
    private static final String EMAILFORM = "liulitry@163.com";
    private static JavaMailSenderImpl mailSender = createMailSender();
	
    /**
     * 邮件发送器
     *
     * @return 配置好的工具
     */
    private static JavaMailSenderImpl createMailSender() {
        JavaMailSenderImpl sender = new JavaMailSenderImpl();
        sender.setHost(HOST);
        sender.setPort(PORT);
        sender.setUsername(USERNAME);
        sender.setPassword(PASSWORD);
        sender.setDefaultEncoding("Utf-8");
        Properties p = new Properties();
        p.setProperty("mail.smtp.timeout", "25000");
        p.setProperty("mail.smtp.auth", "true");
        sender.setJavaMailProperties(p);
        return sender;
    }
    
    /**
     * 发送邮件
     *
     * @param to 接受人
     * @param subject 主题
     * @param html 发送内容
     * @param type 表示发邮件的类别
     * @throws MessagingException 异常
     * @throws UnsupportedEncodingException 异常
     */
    public static void sendHtmlMail(String type,String to, String subject,String code) throws MessagingException,UnsupportedEncodingException {
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        // 设置utf-8或GBK编码，否则邮件会有乱码
        MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage, true, "UTF-8");
        messageHelper.setFrom(EMAILFORM, "系统名称");
        messageHelper.setTo(to);
        messageHelper.setSubject(subject);
        String html="";
        if("login".equals(type)){  //激活用户邮箱
        	html = "尊敬的用户:<br/>你好！<br/>感谢注册永川软件园人才网，我们将竭诚为您服务！请点击：" + "<br/>"
					+ "http://127.0.0.1:8080/crjhr/user_active?code=" + code +"&username="+to
					+ " 激活账户!";
        }else if("openService".equals(type)){  //开启服务
        	html = 	"尊敬的用户:<br/>你好！<br/>感谢使用永川软件园人才网！你在永川软件园人才网申请开通企业招聘服务的资料已通过审核：进入公司主页，完善公司信息"
			+ "<br/>" + "http://www.ycrjhr.com/myCompany.jsp?code="+ code;
        }
        
        
        messageHelper.setText(html, true);
        
        
        
        mailSender.send(mimeMessage);
    }

    
    
}